package ti.me.seek2chat.activities

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View.INVISIBLE
import android.view.View.VISIBLE
import ti.me.seek2chat.adapters.UsersAdapter
import ti.me.seek2chat.databinding.ActivityUsersBinding
import ti.me.seek2chat.leancloud.LeanCloudService
import ti.me.seek2chat.listeners.UserListener
import ti.me.seek2chat.models.User
import ti.me.seek2chat.utilities.Constants
import ti.me.seek2chat.utilities.PreferenceManager

/**
 * UsersActivity 是一个用于显示用户列表的活动类。
 * 它实现了 UserListener 接口，用于处理用户点击事件。
 */
class UsersActivity : BaseActivity(), UserListener {
    private val binding by lazy { ActivityUsersBinding.inflate(layoutInflater) }
    private lateinit var leanCloudService: LeanCloudService
    private lateinit var preferenceManager: PreferenceManager
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        preferenceManager = PreferenceManager(applicationContext)
        leanCloudService = LeanCloudService(applicationContext)
        setListeners()
        getUsers()
    }

    /**
     *  事件绑定
     */
    private fun setListeners() {
        binding.imageBack.setOnClickListener { onBackPressedDispatcher.onBackPressed() }
    }

    /**
     * 获取用户列表并更新 UI。
     */
    private fun getUsers() {
        loading(true)
        val currentUserId = preferenceManager.getString(Constants.KEY_USER_ID)
        leanCloudService.getUsers(currentUserId) { success, users, errorMessage ->
            loading(false)
            if (success && users != null) {
                if (users.isNotEmpty()) {
                    val usersAdapter = UsersAdapter(users, this)
                    binding.usersRecyclerView.adapter = usersAdapter
                    binding.usersRecyclerView.visibility = VISIBLE
                } else {
                    showErrorMessage()
                    Log.d("UsersActivity getUsers", errorMessage.toString())
                }
            } else {
                showErrorMessage()
                Log.d("UsersActivity getUsers", errorMessage.toString())
            }
        }
    }


    /**
     * 显示错误信息。
     */
    private fun showErrorMessage() {
        binding.textErrorMessage.text = String.format("%s", "No users available")
        binding.textErrorMessage.visibility = VISIBLE
    }

    /**
     * 显示或隐藏加载进度条
     *
     * @param isLoading 是否正在加载
     */
    private fun loading(isLoading: Boolean) {
        if (isLoading) {
            binding.progressBar.visibility = VISIBLE
        } else {
            binding.progressBar.visibility = INVISIBLE
        }
    }

    /**
     * 当用户被点击时调用，启动 ChatActivity 并传递用户信息。
     *
     * @param user 被点击的用户
     */
    override fun onUserClicked(user: User) {
        val intent = Intent(applicationContext, ChatActivity::class.java)
        intent.putExtra(Constants.KEY_USER, user)
        startActivity(intent)
        finish()
    }
}